/*******************************************************************************
 * Copyright (c) 2011 LegSem.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * 
 * Contributors:
 *     LegSem - initial API and implementation
 ******************************************************************************/
package com.legsem.legstar.jca.socket;

import javax.resource.ResourceException;
import javax.resource.cci.ConnectionFactory;
import javax.resource.spi.ConnectionManager;
import javax.resource.spi.ConnectionRequestInfo;
import javax.resource.spi.ManagedConnection;
import javax.security.auth.Subject;

import com.legsem.legstar.cci.socket.LegStarSocketConnectionFactoryImpl;
import com.legsem.legstar.jca.LegStarManagedConnectionFactoryImpl;
import com.legstar.csok.client.CicsSocketEndpoint;
import com.legstar.messaging.HostEndpoint;

/**
 * A Managed Factory using Socket connectivity to the mainframe.
 */
public class LegStarSocketManagedConnectionFactoryImpl extends
        LegStarManagedConnectionFactoryImpl {

    private static final long serialVersionUID = -3306904801759008035L;

    public ConnectionFactory createLegStarConnectionFactory(
            ConnectionManager connectionManager) throws ResourceException {
        return new LegStarSocketConnectionFactoryImpl(this, connectionManager);
    }

    /**
     * Create a LegStar Transport endpoint.
     * <p/>
     * <ul>
     * <li>We first use the parameters that were setup at the JNDI level</li>
     * <li>If connection request info was provided we then use that to override
     * the JNDI parameters</li>
     * <li>Finally we make sure defaults are provided</li>
     * </ul>
     * 
     * @param subject the JAAS subject
     * @param connectionRequestInfo additional passed request information
     * @return a LegStar Transport endpoint
     */
    public HostEndpoint createHostEndpoint(Subject subject,
            ConnectionRequestInfo connectionRequestInfo) {
        CicsSocketEndpoint hostEndpoint = new CicsSocketEndpoint();
        hostEndpoint.setHostIPAddress(getServerName());
        hostEndpoint.setHostIPPort(getPortNumber());
        hostEndpoint.setHostUserID(getUserName());
        hostEndpoint.setHostPassword(getPassword());
        hostEndpoint.setHostCharset(getHostCharset());
        hostEndpoint.setConnectTimeout(getHostConnectTimeout());
        hostEndpoint.setReceiveTimeout(getHostReceiveTimeout());
        hostEndpoint.setHostTraceMode(isHostTraceMode());
        return hostEndpoint;
    }

    public ManagedConnection createManagedConnection(
            com.legstar.messaging.LegStarConnection hostConnection,
            HostEndpoint hostEndpoint) {
        return new LegStarSocketManagedConnectionImpl(hostConnection,
                hostEndpoint);
    }

}
